<template>
  <div id="webgl"></div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
import * as THREE from "three";
import earth_img from "../assets/PerspectiveCamera.png";
// 引入gltf模型加载库GLTFLoader.js
import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
import Cesium_Air from "../assets/Cesium_Air.gltf";

// 创建3D场景对象Scene
const scene = new THREE.Scene();

// 一组二维向量表示一个多边形轮廓坐标
const pointsArr = [
  new THREE.Vector2(-50, -50),
  new THREE.Vector2(-60, 0),
  new THREE.Vector2(0, 50),
  new THREE.Vector2(60, 0),
  new THREE.Vector2(50, -50),
];
// Shape表示一个平面多边形轮廓,参数是二维向量构成的数组pointsArr
const shape = new THREE.Shape(pointsArr);
// const geometry = new THREE.ShapeGeometry(shape);
// 拉伸造型
const geometry = new THREE.ExtrudeGeometry(
  shape, //二维轮廓
  {
    depth: 20, //拉伸长度
    bevelThickness: 5,
    bevelSize: 20,
    bevelSegments: 20,
    bevelSegments: 1,
    bevelEnabled: true,
  }
);
const material = new THREE.MeshLambertMaterial();
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);

// 定义threejs输出画布的尺寸(单位:像素px)
const width = window.innerWidth; //宽度
const height = window.innerHeight; //高度
// 实例化一个透视投影相机对象
const camera = new THREE.OrthographicCamera(
  -100 * (width / height),
  100 * (width / height),
  100,
  -100,
  1,
  3000
);
//相机在Three.js三维坐标系中的位置
// 根据需要设置相机位置具体值
camera.position.set(0, 0, 100);
//相机观察目标指向Threejs 3D空间中某个位置
camera.lookAt(0, 0, 0); //指向mesh对应的位置

// AxesHelper：辅助观察的坐标系
const axesHelper = new THREE.AxesHelper(150);
scene.add(axesHelper);

const pointLight = new THREE.PointLight(0xffffff, 1.0);
pointLight.decay = 0.0;
pointLight.position.set(0, 0, 3000);
scene.add(pointLight);
const pointLight2 = new THREE.PointLight(0xffffff, 1.0);
pointLight2.decay = 0.0;
pointLight2.position.set(3000, 0, 0);
scene.add(pointLight2);
const pointLight3 = new THREE.PointLight(0xffffff, 1.0);
pointLight3.decay = 0.0;
pointLight3.position.set(0, 0, -3000);
scene.add(pointLight3);
const pointLight4 = new THREE.PointLight(0xffffff, 1.0);
pointLight4.decay = 0.0;
pointLight4.position.set(-3000, 0, 0);
scene.add(pointLight4);

// 创建渲染器对象
const renderer = new THREE.WebGLRenderer();

renderer.setSize(width, height); //设置three.js渲染区域的尺寸(像素px)
renderer.setClearColor(0xffffff, 1);
renderer.render(scene, camera);

// 设置相机控件轨道控制器OrbitControls
const controls = new OrbitControls(camera, renderer.domElement);
// 如果OrbitControls改变了相机参数，重新调用渲染器渲染三维场景
controls.addEventListener("change", function () {
  renderer.render(scene, camera); //执行渲染操作
}); //监听鼠标、键盘事件

function render() {
  renderer.render(scene, camera); //执行渲染操作
  requestAnimationFrame(render);
}
render();

onMounted(() => {
  document.getElementById("webgl").appendChild(renderer.domElement);
});
</script>
